package com.demo2do.fms.admin.web.controller.supplier.appraisal;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.AppraisalResultSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.AppraisalResultService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.appraisal.AppraisalResult;
import com.demo2do.fms.entity.appraisal.AppraisalResultItem;

/**
 * 绩效结果查询
 * 
 * 每个公司查询自己的绩效考核结果
 * 
 * @author Wy
 * @author Ark
 *
 */
@Controller
@RequestMapping("/appraisal-results")
@MenuSetting("menu-supplier")
public class AppraisalResultController {
    
    @Autowired
    private AppraisalResultService appraisalResultSerive;

    /**
     * list
     * 
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-performance-appraisal-result-search")
    @SuppressWarnings("unchecked")
    public ModelAndView list(@Secure Login login, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-result/appraisal-result-list");
        
        int totals = appraisalResultSerive.count(login);
        if (totals > 0) {
            
            List<AppraisalResult> appraisalResults = appraisalResultSerive.list(login, page.with(totals));
            modelAndView.addObject("appraisalResults", appraisalResults);
            
            List<Long> resultIds = CollectionUtils.extractPropertyList(appraisalResults, "id");
            List<AppraisalResultItem> appraisalResultItems = appraisalResultSerive.listByResult(resultIds);
            
            Map<String, List<AppraisalResultItem>> appraisalResultItemMaps = CollectionUtils.constructMapValues(appraisalResultItems, "result.id");
            modelAndView.addObject("appraisalResultItemMaps", appraisalResultItemMaps);
            
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * search panel
     *
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-result/appraisal-result-search");
        return modelAndView;
    }

    /**
     * search
     * 
     * @param request
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-performance-appraisal-result-search")
    @SuppressWarnings("unchecked")
    public ModelAndView onSearch(AppraisalResultSearch search, @Secure Login login,
                                 HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-result/appraisal-result-list");
        
        int totals = appraisalResultSerive.count(search, login);
        if (totals > 0) {
            
            List<AppraisalResult> appraisalResults = appraisalResultSerive.list(search, login, page.with(totals));
            modelAndView.addObject("appraisalResults", appraisalResults);
            
            List<Long> resultIds = CollectionUtils.extractPropertyList(appraisalResults, "id");
            List<AppraisalResultItem> appraisalResultItems = appraisalResultSerive.listByResult(resultIds);
            
            Map<String, List<AppraisalResultItem>> appraisalResultItemMaps = CollectionUtils.constructMapValues(appraisalResultItems, "result.id");
            modelAndView.addObject("appraisalResultItemMaps", appraisalResultItemMaps);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }

        return modelAndView;
        
    }
    
}
